var authtree,
    $,
    form,
    layer,
    curOrgNode;
UserInfo.init();
$(document).ready(function () {
    $ = layui.jquery;
    form = layui.form;
    layer = layui.layer;
    addEvent(document.getElementById('functionSaveBtn'), 'click', functionSaveCB);
    //加载机构树
    listTree.load("Tree",{});
});

/*
* @author: wj
* @description: 机构选中事件
* @date: 2018/12/24 21:56
* @param
* @return
*/
function treeselected(e) {
    var e = e || window.event;
    // console.log(orgId);
    var param = {
            "cateFunctionTreeView.orgId": e.node.orgId,
            "cateFunctionTreeView.functionCate" : e.node.orgCate,
            "cateFunctionTreeView.orgType": 'org'
        };
    curOrgNode = e.node;
    authtree = layui.authtree;
    // console.log(curOrgNode);
    ActHttpRequest('cateFunctionTreeView/getOrgFunTree.do', param, function(r){
        if(r.OK=="OK"){
            // console.log(r)
            $("#functionSaveBtn").show();
            authtree.render('#functionTree', reloadData(r,"parentId","functionId","functionName"), {
                inputname: 'authids[]'
                ,layfilter: 'lay-check-auth'
                // ,autoclose: false
                // ,autochecked: false
                // ,openchecked: true
                // ,openall: true
                ,autowidth: true
                ,autochecked: false
            });
            //disable(r);
        }
    });
}
/**
 * 数据封装
 * @param data 数据
 * @param pid  父ID
 * @param id  ID
 * @param name 显示名称
 */
function reloadData(data, pid, id, name){
    var listobj = [],
        items = {};
    for(var i=0;i<data.List.length;i++){
        var obj= data.List[i];
        var parentId="root"
        if(obj[pid] && obj[pid]!="" && obj[pid]!="0"){
            parentId=obj[pid]
        }
        var jo={
            "id" : obj[id],
            "value" : obj[id],
            "name" : obj[name],
            "parentId" : parentId,
            "node": obj,
            "checked" : obj["checked"]
        };

        if(items[parentId]){
            items[parentId].push(jo);
        }else{
            items[parentId] = [];
            items[parentId].push(jo);
        }
    }
    return formatTree(items, "root");
}

/**
 * 递归每个节点
 */
function formatTree(items, parentId) {
    var result = [];
    if (!items[parentId]) {
        return result;
    }
    for (var i=0;i<items[parentId].length;i++){
        var t=items[parentId][i];
        t["list"] = formatTree(items, t.id)
        result.push(t);
    }
    return result;
}
/*
* @author: glx
* @description: 根据数据判断对继承节点禁用
* @date: 2018/12/28 9:55
* @param
* @return
*/
function disable(data) {
    for(var i=0;i<data.List.length;i++) {
        var obj = data.List[i];

        if (obj.checked && obj.inherit == "true") {
            document.getElementById(obj["functionId"]).disabled = "disabled";
        } else {
            document.getElementById(obj["functionId"]).disabled = "";
        }
    }
}
/*
* @author: wj
* @description: 授权功能保存
* @date: 2018/12/24 15:06
* @param
* @return
*/
function functionSaveCB(){
    var checked = authtree.getChecked('#functionTree'),
        functionId = "";
    for (var i = 0; i < checked.length; i++) {
        var curretnNode =eval("("+checked[i]+")");
        functionId = functionId + "," + curretnNode.functionId;
    }
    var formData = {
        'sysPrivilege.functionId': functionId.substring(1),
        'sysPrivilege.privilegeType': "org",
        'sysPrivilege.billId': curOrgNode.orgId,
        'sysPrivilege.orgId': curOrgNode.orgId
    };
    console.log(formData)
    ActHttpRequest("sysPrivilege/add.do", formData, function (data) {
        if (data.OK == "OK") {
            layer.msg("保存成功！");
        } else {
            layer.alert(data.message);
        }
    });
}
